package com.marktotop.response;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * 公共返回对象
 *
 * @author WXLD
 * @data 2021/6/6 - 9:06
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultBean {


    private  Integer code;

    private  String message;

    private  Boolean success;

    private Map<String,Object> data = new HashMap<String, Object>();




    public static ResultBean ok(){
        ResultBean r = new ResultBean();
        r.setSuccess(ResultCode.SUCCESS.getSuccess());
        r.setCode(ResultCode.SUCCESS.getCode());
        r.setMessage(ResultCode.SUCCESS.getMessage());
        return r;
    }

    /**
     * @return 返回失败的结果集
     */
    public static ResultBean error(){
        ResultBean r = new ResultBean();
        r.setSuccess(ResultCode.ERROR.getSuccess());
        r.setCode(ResultCode.ERROR.getCode());
        r.setMessage(ResultCode.ERROR.getMessage());
        return r;
    }


    /**
     * 返回成功的结果集 (带数据)
     * @param resultCode
     * @return
     */
    public static ResultBean result(ResultCode resultCode){
        ResultBean r = new ResultBean();
        r.setSuccess(resultCode.getSuccess());
        r.setCode(resultCode.getCode());
        r.setMessage(resultCode.getMessage());
        return r;
    }

    public ResultBean message(String message){
        this.setMessage(message);
        return this;
    }
    public ResultBean data(String key, Object value){
        this.data.put(key, value);
        return this;
    }
}
